package com.finalproject.endice9x9;

import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

//Game fragment that compounds Game board fragment and Game control fragment
//Initializes game controller and model and link them together
//Supports Option menu
public class GameFragment extends Fragment {
	//Possible game states
	enum GAME_MODE{
		PLAY,
		EDIT,
		NEW
	}
	private GameController controller;
	private GAME_MODE gameMode = GAME_MODE.PLAY;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_game, parent, false);
		//Create game controller and pass context
		controller = new GameController(getActivity());
		//Create control and game board fragments	
		GameBoardFragment fragmentBoard = new GameBoardFragment(controller);		
		GameControlFragment fragmentControl = new GameControlFragment(controller);	
		//Create model
		GameModel model = new GameModel();
		//Pass views and game model to controller
		controller.setViewModel(fragmentBoard, fragmentControl, model);			
		//Get fragment manager and add corresponding fragments to their containers
		FragmentManager fm = getActivity().getFragmentManager();
		fm.beginTransaction().
			replace(R.id.fragmentContainerControl, fragmentControl).
			replace(R.id.fragmentContainerBoard, fragmentBoard).
			commit();
		
		return v;
	}
	
	@Override
	public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
	    super.onCreateOptionsMenu(menu, inflater);
	    inflater.inflate(R.menu.game_board_menu, menu);
	    // hide unrelated menu items 
	    menu.findItem(R.id.action_menu_save).setVisible(false);
	    menu.findItem(R.id.action_menu_cancel).setVisible(false);
	}

	//Process menu item, change game mode and hide unrelated items
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		ActionBar ab = getActivity().getActionBar();
		switch (item.getItemId()) {
        case R.id.action_menu_addnew:     	
            ab.setSubtitle("New Level");
            gameMode = GAME_MODE.NEW;
            getActivity().invalidateOptionsMenu();
            controller.newLevel();           
            return true;
            
        case R.id.action_menu_delete:
        	ab.setSubtitle("");
        	gameMode = GAME_MODE.PLAY;
        	getActivity().invalidateOptionsMenu();
        	controller.deleteLevel();         
            return true;
            
        case R.id.action_menu_edit:       	
            ab.setSubtitle("Edit Level"); 
            gameMode = GAME_MODE.EDIT;
            getActivity().invalidateOptionsMenu();
            controller.editLevel();           
            return true;
            
        case R.id.action_menu_save:
        	ab.setSubtitle("");
        	gameMode = GAME_MODE.PLAY;
        	getActivity().invalidateOptionsMenu();
        	controller.saveLevel();            
            return true;
            
        case R.id.action_menu_cancel:
        	ab.setSubtitle("");
        	gameMode = GAME_MODE.PLAY;
        	getActivity().invalidateOptionsMenu();
        	controller.cancelLevel();
        	return true;
        	
        default:
            return super.onOptionsItemSelected(item);
		}
	}

	//Hide unrelated items
	@Override
	public void onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
		switch(gameMode){
		case EDIT:
		case NEW:
			menu.findItem(R.id.action_menu_save).setVisible(true);
		    menu.findItem(R.id.action_menu_cancel).setVisible(true);
		    menu.findItem(R.id.action_menu_addnew).setVisible(false);
		    menu.findItem(R.id.action_menu_edit).setVisible(false);
		    menu.findItem(R.id.action_menu_delete).setVisible(false);
			break;
			
		case PLAY:
			menu.findItem(R.id.action_menu_save).setVisible(false);
		    menu.findItem(R.id.action_menu_cancel).setVisible(false);
		    menu.findItem(R.id.action_menu_addnew).setVisible(true);
		    menu.findItem(R.id.action_menu_edit).setVisible(true);
		    menu.findItem(R.id.action_menu_delete).setVisible(true);
			break;
			
		}	
	}

	//Save current game model state when fragment is destroyed 
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		controller.saveCurrentState();
	}
}
